//
//  EvaluationDetailViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/8/7.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "GKPhotoBrowser.h"
#import "SDImageCache.h"

#import "EvaDetailUserInfoCell.h"

#import "PostDetailImageCell.h"
#import "PostDetailTextCell.h"

#import "DetailTextCell.h"
#import "DetailImageViewCell.h"

#import "EvaluateDetailPackageCell.h"

#import "ProductDetailEvaluationCell.h"


#import "EvaluationDetailViewController.h"

@interface EvaluationDetailViewController ()<UITableViewDelegate, UITableViewDataSource,GKPhotoBrowserDelegate>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) CommonEvaluateModel *model;

@end

@implementation EvaluationDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    
    [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
        if (isLogin) {
            [self request_evaluateDetail];
        }
    }];
}

- (void)initView {

    self.title = @"评价详情";
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
}

#pragma mark- 协议方法
#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case EvaluationDetailSectionType_userInfo:{
            return 1;
        } break;
        case EvaluationDetailSectionType_text:{
            return 1;
        } break;
        case EvaluationDetailSectionType_image:{
            return self.model.imageUrls.count;
        } break;
        default:{
        } break;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case EvaluationDetailSectionType_userInfo:{
            return 60;
        } break;
        case EvaluationDetailSectionType_text:{
            return self.model.evaluateTextHeight + 40;
        } break;
        case EvaluationDetailSectionType_image:{
            return self.model.imageUrls[indexPath.row].itemHeight;
        } break;
        default:{
        } break;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case EvaluationDetailSectionType_userInfo:{

            EvaDetailUserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EvaDetailUserInfoCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            [cell fillCellWithModel:self.model];
            return cell;

        } break;
        case EvaluationDetailSectionType_text:{

            PostDetailTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PostDetailTextCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            cell.contentLab.text = self.model.ueEvaluateContent;
            return cell;

        } break;
        case EvaluationDetailSectionType_image:{

            PostDetailImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PostDetailImageCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
                            
                WEAKSELF
                [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:self.model.imageUrls[indexPath.row].url] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    
                    if (image) {
                        weakSelf.model.imageUrls[indexPath.row].itemHeight = (image.size.height*(kScreenWidth-28)/image.size.width) + 10;
                    } else {
                        weakSelf.model.imageUrls[indexPath.row].itemHeight = TTScreenWidth - 20;
                    }
                    
                    [weakSelf.tableview reloadRow:indexPath.row inSection:indexPath.section withRowAnimation:UITableViewRowAnimationNone];
                }];
            
            return cell;

        } break;
        default:{
        } break;
    }

    PostDetailTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PostDetailTextCellID"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case EvaluationDetailSectionType_userInfo:{
        } break;
        case EvaluationDetailSectionType_text:{
        } break;
        case EvaluationDetailSectionType_image:{
            
            if (self.model.imageUrlArray.count > 0) {
                [self showImageWithArray:self.model.imageUrlArray index:indexPath.row];
            } else {
                [SVProgressHUD showErrorWithStatus:@"无图片"];
                [SVProgressHUD dismissWithDelay:1];
            }
            
        } break;
        default:{
        } break;
    }
}

- (void)showImageWithArray:(NSArray *)imageUrls index:(NSInteger)index {
    NSMutableArray *photos = [NSMutableArray new];
    
    [imageUrls enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        GKPhoto *photo = [GKPhoto new];
        photo.url = [NSURL URLWithString:obj];
        [photos addObject:photo];
    }];
    
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:index];
    browser.showStyle = GKPhotoBrowserShowStyleNone;
    browser.hideStyle = GKPhotoBrowserHideStyleZoomScale;
    
    browser.delegate  = self;
    [browser showFromVC:self];
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"EvaDetailUserInfoCell" bundle:nil] forCellReuseIdentifier:@"EvaDetailUserInfoCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"PostDetailImageCell" bundle:nil] forCellReuseIdentifier:@"PostDetailImageCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"PostDetailTextCell" bundle:nil] forCellReuseIdentifier:@"PostDetailTextCellID"];
        
    }
    return _tableview;
}

- (void)request_evaluateDetail {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.ueId forKey:@"ueId"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_GetEvaluateDetail];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                 
                    weakSelf.model = [CommonEvaluateModel modelWithDictionary:resultDic];
                    
                    weakSelf.model.infoHeight = 60.0f;
                    weakSelf.model.evaluateTextHeight = [Utilities getStringHeightWithText:weakSelf.model.ueEvaluateContent font:[UIFont systemFontOfSize:12] viewWidth:TTScreenWidth-30];

                     NSArray *imageUrls = [resultDic valueForKey:@"imageUrls"];
                     if ([imageUrls isKindOfClass:[NSArray class]]) {
                         weakSelf.model.imageUrls = @[].mutableCopy;
                         weakSelf.model.imageUrlArray = @[].mutableCopy;
                         for (int j = 0; j < imageUrls.count; j++) {
                             CommonImageModel *imgModel = [CommonImageModel modelWithDictionary:imageUrls[j]];
                             imgModel.itemHeight = 200;

                             if (imgModel.url.length > 0) {
                                 [weakSelf.model.imageUrlArray addObject:imgModel.url];
                             }
                             
                             [weakSelf.model.imageUrls addObject:imgModel];
                         }

                     }
                    [weakSelf.tableview reloadData];
                }
                
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
    
}

@end
